﻿using AutoMapper;
using MediatR;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.OutboundOrder;
using THE_WIND_RISES.Common.Dtos;

namespace Production_Set.API.MaterialManagement.Add
{
    public class OutBundCreateHandler : IRequestHandler<OutBundCreateCommon, Result>
    {
        private readonly IBaseRepository<OutBound> OutBoundRepository;
        public IMapper mapper { get; set; }
        public OutBundCreateHandler(IBaseRepository<OutBound> outBoundRepository, IMapper mapper)
        {
            OutBoundRepository = outBoundRepository;
            this.mapper = mapper;
        }

        public async Task<Result> Handle(OutBundCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCodes.Added };
            var AddData = mapper.Map<OutBound>(request);
            string sql = "insert into OutBound values(@BoundId,@BoundNo,@BoundType,@WarehouseId,@WarehouseAddress,@BoundRemark,@State" +
                ",@BoundSource,@SourceNo,@SourceName,@Customer,@Contacts,@BoundDate,@BoundState,@BoundPeople,@BoundDepartment,@Handle,@HandleContext,@HandleTime,@Pid,@IsDelete);";
            var AddNum = OutBoundRepository.Add(sql, new
            {
                AddData.BoundId,
                AddData.BoundNo,
                AddData.BoundType,
                AddData.WarehouseId,
                AddData.WarehouseAddress,
                AddData.BoundRemark,
                AddData.State,
                request.BoundSource,
                AddData.SourceNo,
                AddData.SourceName,
                AddData.Customer,
                AddData.Contacts,
                request.BoundDate,
                AddData.BoundState,
                AddData.BoundPeople,
                AddData.BoundDepartment,
                AddData.Handle,
                AddData.HandleContext,
                AddData.HandleTime,
                AddData.Pid,
                AddData.IsDelete,
            });
            if (AddNum > 0)
            {
                result.Code = (int)CommonErrorCodes.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
